package org.batch.config.batch.jobs;

import org.batch.config.batch.listener.BatchListener;
import org.springframework.batch.core.Job;
import org.springframework.batch.core.Step;
import org.springframework.batch.core.configuration.annotation.JobBuilderFactory;
import org.springframework.batch.core.launch.support.RunIdIncrementer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.DependsOn;

import javax.annotation.Resource;

/**
 * 批处理任务定义配置类
 *
 * @author LiKun
 * @date 2021/9/29 17:29
 */
@Configuration
public class BatchJobConfig {
    @Resource
    private BatchListener batchListener;

    /**
     * 定义CSV Job任务
     *
     * @param jobBuilderFactory 工厂类
     * @param csvStep           批处理接口
     *
     * @return job
     */
    @Bean("csvJob")
    @DependsOn("csvStep")
    public Job csvJob(JobBuilderFactory jobBuilderFactory, Step csvStep) {
        return jobBuilderFactory.get("csvJob")
                .incrementer(new RunIdIncrementer())
                .flow(csvStep)
                .end()
                .listener(batchListener)
                .build();
    }

    /**
     * 定义MySQL Job任务
     *
     * @param jobBuilderFactory 工厂类
     * @param mysqlStep         批处理接口
     *
     * @return job
     */
    @Bean("mysqlJob")
    @DependsOn("mysqlStep")
    public Job mysqlJob(JobBuilderFactory jobBuilderFactory, Step mysqlStep) {
        return jobBuilderFactory.get("mysqlJob")
                .incrementer(new RunIdIncrementer())
                .flow(mysqlStep)
                .end()
                .listener(batchListener)
                .build();
    }
}
